<?php
/**
 * Project:     drylamp - rapid application development framework for php
 * File:       SessionHandler.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * 
 * This is an abstract class for custom session handlers
 * 
 * @copyright 2009-2010 Ilya St.
 * @author Ilya St. <provided at gmail dot com>
 * @version 1.0
 */

abstract class SessionHandler {

	/**
	 * properties below correspond to session settings in php.ini
	 */
	
	public $session_save_handler;
	public $session_save_path;
	public $session_use_cookies; 
	public $session_name;
	public $session_auto_start;
	public $session_cookie_lifetime;
	public $session_cookie_path;
	public $session_cookie_domain;  
	public $session_cookie_httponly;
	public $session_serialize_handler;
	public $session_gc_probability;
	public $session_gc_divisor;
	public $session_gc_maxlifetime;
	public $session_bug_compat_42;
	public $session_bug_compat_warn;
	public $session_referer_check;
	public $session_entropy_length;
	public $session_entropy_file;
	public $session_cache_limiter;
	public $session_cache_expire;   
	public $session_use_trans_sid;
	
  /**
   * 	 Select a hash function
	 *	 0: MD5   (128 bits)
	 *	 1: SHA-1 (160 bits)
   */
	protected $session_hash_function;
	protected $session_hash_bits_per_character;

  protected $exceptionsLogger;
	
	/**
	 * Creates a session handler object and applies current configation to it.
	 */
	public function __construct() {
		$this->session_save_handler 		 = ini_get('session.save_handler');
		$this->session_save_path 				 = ini_get('session.save_path');
		$this->session_use_cookies 			 = ini_get('session.use_cookies');
		$this->session_name 						 = ini_get('session.name');
		$this->session_auto_start 			 = ini_get('session.auto_start');
		$this->session_cookie_lifetime 	 = ini_get('session.cookie_lifetime');
		$this->session_cookie_path 			 = ini_get('session.cookie_path');
		$this->session_cookie_domain 	 	 = ini_get('session.cookie_domain');
		$this->session_cookie_httponly 	 = ini_get('session.cookie_httponly');
		$this->session_serialize_handler = ini_get('session.serialize_handler');
		$this->session_gc_probability 	 = ini_get('session.gc_probability');
		$this->session_gc_divisor 			 = ini_get('session.gc_divisor');
		$this->session_gc_maxlifetime    = ini_get('session.gc_maxlifetime');
		$this->session_bug_compat_42 		 = ini_get('session.bug_compat_42');
		$this->session_bug_compat_warn 	 = ini_get('session.bug_compat_warn');
		$this->session_referer_check 		 = ini_get('session.referer_check');
		$this->session_entropy_length 	 = ini_get('session.entropy_length');
		$this->session_entropy_file 		 = ini_get('session.entropy_file');
		$this->session_cache_limiter 		 = ini_get('session.cache_limiter');
		$this->session_cache_expire 		 = ini_get('session.cache_expire');
		$this->session_use_trans_sid 		 = ini_get('session.use_trans_sid');		
	}
	
	
	/**
	 * "Opens" the session data storage for usage. 
	 * @param unknown_type $save_path
	 * @param unknown_type $session_name
	 */
	abstract public function open( $save_path, $session_name );
		
	
	/**
	 * Closes session data storage
	 */
	abstract public function close() ;
		
	
	/**
	 * Reads the data record, that corresponds to the session_id
	 * @param unknown_type $session_id
	 */
	abstract public function read( $session_id ); 		
	
	/**
	 * Stores the data in the storage
	 * @param unknown_type $session_id
	 * @param unknown_type $session_data
	 */
	abstract public function write( $session_id, $session_data ); 
		
	/**
	 * Deletes the record, that corresponds to the session_id
	 * @param unknown_type $session_id
	 */
	abstract public function destroy( $session_id ); 
		
	/**
	 * Deletes old session data records, that are older than $max_expire_time
	 * @param unknown_type $max_expire_time
	 */
	abstract public function gc( $max_expire_time ); 
	
	
	public function setExceptionsLogger(DryAbstractLogger $pLogger) {
		$this->exceptionsLogger = $pLogger;
	}
	
	/**
	 * Default exceptions handler - logs exceptions into the file /var/log/drylamp_session.log
	 * @param Exception $e
	 * @throws Exception
	 * @return boolean
	 */
	protected function handleException(Exception $e) {
	  	if ( $this->exceptionsLogger instanceof DryAbstractLogger ) {
	  		return $this->exceptionsLogger->logException($e);	  		
	  	}
		  throw $e;
	}
		
}